//
//  StrategyViewController.m
//  wedding
//
//  Created by 常宽 on 15/1/24.
//  Copyright (c) 2015年 常宽. All rights reserved.
//

#import "StrategyViewController.h"
#import "StrategyModel.h"   // model,和上级一样
#import "StrategySecViewController.h"   // 下级页面

@interface StrategyViewController ()<UITableViewDelegate, UITableViewDataSource>
/// 列表
@property (nonatomic, retain) UITableView *tableView;
/// 数据数组
@property (nonatomic, retain) NSMutableArray *dataArray;

@end

@implementation StrategyViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        self.oldModel = [[StrategyModel alloc] init];
        self.dataArray = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void)netWorking
{
    NSString *string = [NSString stringWithFormat:@"http://data.halobear.cn/mapi/index.php?act=articlecate&pid=%@", self.oldModel.cate_id];
    [NetHandler networkWithURL:string completion:^(id result) {
        NSArray *array = [result objectForKey:@"list"];
        for (NSDictionary *dic in array) {
            StrategyModel *model = [[StrategyModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataArray addObject:model];
        }
        [self.tableView reloadData];
    }];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 二级页面隐藏tabBar
    self.tabBarController.tabBar.hidden = YES;
    // 重写返回上级页面方法
    UIImage *backImage = [[UIImage imageNamed:@"返回.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem= [[UIBarButtonItem alloc] initWithImage:backImage style:UIBarButtonItemStylePlain target:self action:@selector(popBack:)];
    [self netWorking];
    [self createTableView];
    
}
// 返回上级页面
- (void)popBack:(UIBarButtonItem *)sender
{
//    [self.tabBarController.tabBar setHidden:NO];//上级页面不是首页不需要显示tabBar;
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)createTableView
{
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:(UITableViewStylePlain)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
}

#pragma mark -
#pragma mark tabelView协议
///  section 个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
///  每个section里的row的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
///  返回tableView里的cell样式
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *string = @"StrategySec";
    BaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:string];
    if (!cell) {
        cell = [[BaseTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
    }
    cell.textLabel.text = [self.dataArray[indexPath.row] cate_name];
    return cell;
}
/// 返回row的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}
///  选中cell执行的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    StrategySecViewController *secVC = [[StrategySecViewController alloc] init];
    secVC.oldModel = self.dataArray[indexPath.row];
    [self.navigationController pushViewController:secVC animated:YES];
}

#pragma mark -





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
